import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import {SessionDto} from './dto/session.dto'
import { Session} from './entities/session.entity'
@Injectable()
export class SessionService {
  constructor(
    @InjectModel('Sessions') private readonly sessionModel:Model<Session>
) {}
  async create(sessionDto: SessionDto) {
    return await this.sessionModel.insertMany(sessionDto);
  }
  async findOne(sessionName: string, id: string):Promise<SessionDto[]> {
    if(id)return await this.sessionModel.find({_id:id});
    if(sessionName)return await this.sessionModel.find({sessionName:{$regex:sessionName}});
  }

  async update(id: string, sessionDto: SessionDto) {
    return await this.sessionModel.updateOne({_id:id}, sessionDto);
  }

  async remove(id: string):Promise<any> {
    return await this.sessionModel.deleteOne({_id:id});
  }

  async list(page: number, pageSize: number, userId: string):Promise<any> {
    const skip = (page - 1) * pageSize;
    const data = await this.sessionModel.find({userId: userId}).sort({ time: -1 })
    // const list = await this.sessionModel.find({userId: userId}).sort({ time: -1 }).skip(skip).limit(pageSize)
    return {total: data.length, list: data}
  }
}
